
import 'dart:convert';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dali_ng_pera/manager/dnp_cache_manager.dart';
import 'package:dali_ng_pera/global/dnp_global_manager.dart';
import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/mob/dnp_mob.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/page/main/dnp_main_page.dart';
import 'package:dali_ng_pera/page/welcome/dnp_welcome_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import '../../net/dnp_http_result.dart';
import 'dnp_splash_entity.dart';
import 'package:dio/dio.dart' as dio;

class DnpSplashController extends GetxController {

  bool _isInit = false;

  bool isRefreshing = false;

  final Connectivity _netConnectivity = Connectivity();

  bool _connectivityFlag = false;

  DnpSplashEntity? splashEntity;

  @override
  void onInit() {
    super.onInit();
    // initializeData();

    initIPadData();

  }

  void initIPadData() async {
    bool isPad = await DnpPluginManager().plugin.isPad;

    if (isPad) {
      var status = await _netConnectivity.checkConnectivity();
      if (status == ConnectivityResult.none) { // 没有网络
        _pushReplacement(DnpMainPage());
      }else {
        displayWelcome();
      }
    }else {
      Future.delayed(Duration(milliseconds: 350),(){
        checkNetwork();
      });
    }
  }

  void initializeData() async {

    isRefreshing = true;

    DnpPluginManager().showLoading();

    DnpHttpResult result = await DnpNetManager().post(
      '/haseye/autou',
      data: dio.FormData.fromMap({
        'autou' : "en",
        "di" : "0",
        "christos" : "0"
      }),
    );

    isRefreshing = false;

    DnpPluginManager().hideLoading();

    if (result.success) {

      DnpSplashEntity entity = DnpSplashEntity.fromJson(result.close);

      splashEntity = entity;

      DnpGlobalManager().setSplashEntity(entity);

      DnpPluginManager().submitFB(entity.kai);

      displayWelcome();

      uploadMob();
      pullCityData();
    }else {
      DnpPluginManager().showToast(result.kurios ?? "Network Error");
    }
  }

  void checkNetwork() async {
    List<ConnectivityResult> connectivityResult = await _netConnectivity.checkConnectivity();
    if (connectivityResult.contains(ConnectivityResult.none)) {
      checkNetworkTracking();
    }else {
      hasNetworkStatus(true);
    }
  }

  void checkNetworkTracking() {
    _netConnectivity.onConnectivityChanged.listen((result) {
      hasNetworkStatus(!result.contains(ConnectivityResult.none));
    });
  }

  void hasNetworkStatus(bool flag) {
    if (flag && (splashEntity == null)) {
      if (!isRefreshing) {
        initializeData();
      }
    }else {
      DnpPluginManager().hideLoading();
    }
  }

  void displayWelcome() {
    bool hasWelcome = DnpCacheManager().getBool("has_welcome");

    if (hasWelcome) {
      _pushReplacement(DnpMainPage());
    }else {
      _pushReplacement(DnpWelcomeWidget());
    }
  }

  void _pushReplacement(Widget widget) {
    if (DnpGlobalManager().ctx != null) {
      Navigator.pushReplacement(
        DnpGlobalManager().ctx!,
        PageRouteBuilder(
          pageBuilder: (_, __, ___) => widget,
          transitionDuration: Duration.zero,
        ),
      );
    }
  }

  void pullCityData(){
    DnpNetManager().get("/haseye/enchantment").then((value){
      if(value.close != null){
        DnpCacheManager().saveString("enchantment", jsonEncode(value.close));
      }
    });
  }

  void uploadMob() {
    DnpMob.indescribable();
    DnpMob.fervour();
  }
}